package yl.hs.bmipfull.controllers.production;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import yl.hs.bmipfull.controllers.ControllerBase;
import yl.hs.bmipfull.pojo.salemanage.SaleOrderEntity;
import yl.hs.bmipfull.service.production.IProductionTaskFertilizerService;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.SubmitModel;
import yl.hs.bmipfull.viewmodels.WebResult;
import yl.hs.bmipfull.viewmodels.production.TransferList;
import yl.hs.bmipfull.viewmodels.salemanmage.SaleTaskModel;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作用:  控制层<br/>
 * 创建时间: 2023年06月07日 15时26分04秒 <br/>
 *
 * @author 吴高丰
 */
@Api(value = "Web接口", tags = "管理--操作")
@RequestMapping(value = "/api/productiontaskfertilizer")
@RestController
public class ProductionTaskFertilizerController extends ControllerBase {
    @Autowired
    IProductionTaskFertilizerService service;
    @ApiOperation(value = "查询", notes = "查询")
    @RequestMapping(value = "", method = RequestMethod.POST)
    public WebResult queryList(@ApiParam(value = "查询条件") @RequestBody PageQuery pageQuery) {
        try {
            return success("查询成功", service.queryList(pageQuery));
        } catch (Exception e) {
            e.printStackTrace();
            return fail("查询失败,查询条件异常!");
        }
    }
    @ApiOperation(value = "根据sn更改生产任务状态", notes = "根据sn更改生产任务状态")
    @RequestMapping(value = "task/update/{sn}/{state}", method = RequestMethod.GET)
    public WebResult updateTask(@ApiParam(value = "生产任务sn") @PathVariable String sn,
                                @ApiParam(value = "生产任务状态") @PathVariable Short state) {
        if (service.updateTask(sn, state) > 0) {
            return success("修改成功");
        } else {
            return fail("修改失败");
        }
    }
    @ApiOperation(value = "查询", notes = "分页查询")
    @RequestMapping(value = "/{rowNum}/{page}", method = RequestMethod.POST)
    public WebResult queryPage(@RequestBody PageQuery query,
                               @ApiParam(value = "页码") @PathVariable int page,
                               @ApiParam(value = "每页条数") @PathVariable int rowNum) {
        try {
            var result = service.queryPage(query, rowNum, page);
            return success("ok", result);
        } catch (Exception ee) {
            return error(ee);
        }
    }
    @ApiOperation(value = "查询", notes = "根据编号查询")
    @RequestMapping(value = "snList/{snList}", method = RequestMethod.GET)
    public WebResult findBySNList(@ApiParam(value = "编号") @PathVariable String snList) {
        return success("查询成功", service.findBySNList(snList));
    }
    @ApiOperation(value = "查询", notes = "根据编号查询")
    @RequestMapping(value = "/{sn}", method = RequestMethod.GET)
    public WebResult findBySn(@ApiParam(value = "编号") @PathVariable String sn) {
        return success("查询成功", service.findBySn(sn));
    }


    @ApiOperation(value = "保存", notes = "查询/更新信息")
    @RequestMapping(value = "", method = RequestMethod.PUT)
    public WebResult saveMenu(@ApiParam(value = "提交信息") @RequestBody SubmitModel submitModel) {
        Integer result;
        try {
            result = service.saveproduction_task_fertilizer(submitModel.getData(), submitModel.getSn());
            if (result > 0) {
                return success("保存成功");
            } else {
                return fail("数据未保存");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return fail("数据未保存");
        }

    }

    @ApiOperation(value = "删除信息", notes = "根据编号删除系信息")
    @RequestMapping(path = "/{sn}", method = RequestMethod.DELETE)
    public WebResult delete(@ApiParam(value = "编号", name = "sn", allowMultiple = true, required = true)@PathVariable String sn) {
        if (service.delete(sn) > 0) {
            return success("删除成功");
        } else {
            return fail("删除失败");
        }
    }
    @ApiOperation(value = "根据销售订单生成生产任务", notes = "根据销售订单生成生产任务")
    @RequestMapping(value = "/savabyorder", method = RequestMethod.PUT)
    public WebResult savaByorder(@ApiParam(value = "提交信息") @RequestBody SaleTaskModel data) {
        Integer result;
        try {
            result = service.saveTaskBySaleOrder(data.getData());
            if (result > 0) {
                return success("保存成功");
            } else if(result == -1){
                return fail("创建失败，订单已被创建");
            }else{
                return fail("数据未保存");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return fail("数据未保存");
        }

    }
    @ApiOperation(value = "查询订单详情", notes = "查询订单详情")
    @RequestMapping(path = "/changeState/{sn}/{state}", method = RequestMethod.GET)
    public WebResult changeState(@ApiParam(value = "编号", name = "sn", allowMultiple = true, required = true) @PathVariable  String sn,@ApiParam(value = "状态", name = "state", allowMultiple = true, required = true) @PathVariable  short state) {
        var result = service.changeBySNList(sn,state);
        return success("查询成功",result);
    }
    @ApiOperation(value = "查询订单详情", notes = "查询订单详情")
    @RequestMapping(path = "/queryOrderDetail/{sn}", method = RequestMethod.GET)
    public WebResult queryOrderDetail(@ApiParam(value = "编号", name = "sn", allowMultiple = true, required = true) @PathVariable  String sn) {
        var list = Arrays.stream(sn.split(",")).toList();
        var result = service.queryOrderDetail(list);
        return success("查询成功",result);
    }
    @ApiOperation(value = "查询生产任务所需原材数量信息", notes = "查询生产任务所需原材数量信息")
    @RequestMapping(path = "/GetRmQueary/{taskSn}", method = RequestMethod.GET)
    public WebResult GetRmQueary(@PathVariable String taskSn){
        var result =  service.GetRmQueary(taskSn);
        return success("查询成功",result);
    }
    @ApiOperation(value = "生产任务转生产", notes = "生产任务转生产")
    @RequestMapping(path = "/transferProduction", method = RequestMethod.POST)
    public WebResult transferProduction(@RequestBody TransferList model){
        var result =  service.transferProduction(model);
        return success("查询成功",result);
    }
}